package com.mp.approvalreq.util;


import com.managerplus.key.service.NumberBuilderServicePrx;
import com.mp.approvalreq.entity.Request;
import com.plusmoney.exception.RpcServerLogicalException;
import com.qq.tars.client.util.ServantUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 审批编号生成器
 */
public class NumberBuilder {

    /**
     * getAutoNum() : 只能生成1-999,9999的位数,一天能生成超过999,9999条的审批也不可能
     */
    public static String getApprovalNum() throws Throwable {
        return PREFIX + getDateNum() + "0" + getAutoNum();
    }

    // [200] + [20180323] + 8位自增，不够前面补0
    private static final int PREFIX = 200;

    private static String getDateNum() {
        SimpleDateFormat sdf = DateUtils.getSdf(DateUtils.YMD);
        String date = sdf.format(System.currentTimeMillis());
        return date.replaceAll("-", "");
    }

    private static String getAutoNum() throws Throwable {
//        AutoNumberServicePrx autoNum = ServiceUtils.getAutoNumService();
//        return autoNum.getAutoIncrementNo(PREFIX);
        NumberBuilderServicePrx numberBuilderService = ServantUtils.getObject(NumberBuilderServicePrx.class);
        return numberBuilderService.buildNumber(PREFIX);
    }


    private static void buildApprovalNum(Request req) throws Exception {
        checkParam(req);
        req.setApprovalNum(generateNum(req));
    }

    private static void buildApprovalNum(List<Request> reqList) throws Exception {
        if (checkParam(reqList)) {
            SimpleDateFormat sdf = DateUtils.getSdf(DateUtils.YMDHM);
            for (Request req : reqList) {
                req.setApprovalNum(generateNum(req, sdf));
            }
        }
    }

    private static void checkParam(Request req) throws Exception {
        if (StringUtils.isEmpty(req.getRequestTime())) {
            throw new RpcServerLogicalException("The Request has't create time!");
        }

        if (req.getRequestDkey() == 0 || req.getApplicant() == 0) {
            throw new RpcServerLogicalException("The Request has't Dkey or applicant!");
        }
    }

    private static boolean checkParam(List<Request> reqList) throws Exception {
        if (ListHelper.isNullEmpty(reqList)) {
            return false;
        }
        for (Request req : reqList) {
            checkParam(req);
        }
        return true;
    }

    private static String generateNum(Request req) throws ParseException {
        SimpleDateFormat sdf = DateUtils.getSdf(DateUtils.YMDHM);
        long time = sdf.parse(req.getRequestTime()).getTime();
        int applicantId = req.getApplicant();
        int reqDkey = req.getRequestDkey();
        String charac = character[getIndex(reqDkey)];
        StringBuilder res = new StringBuilder();
        return res.append(charac).append(applicantId).append(charac).append(time).append(charac).append(reqDkey).toString();
    }

    private static String generateNum(Request req, SimpleDateFormat sdf) throws ParseException {
        long time = sdf.parse(req.getRequestTime()).getTime();
        int applicantId = req.getApplicant();
        int reqDkey = req.getRequestDkey();
        String charac = character[getIndex(reqDkey)];
        StringBuilder res = new StringBuilder();
        return res.append(charac).append(applicantId).append(charac).append(time).append(charac).append(reqDkey).toString();
    }


    private static final String character[] =
     {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
      "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
      "U", "V", "W", "X", "Y", "Z"};

    /**
     * A : 0-31
     * B : 32-63
     * 递增32为一个临界点
     */
    private static int getIndex(int reqDkey) {
        int index = reqDkey < 0 ? (-reqDkey) : reqDkey;
        while (index >= 26) {
            index >>= 5;
        }
        return index;
    }

}
